#!/bin/bash

# SPAM3UK API 快速验证脚本
# 针对Trim功能问题进行专项检查

RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
NC='\033[0m'

SERVER_HOST="101.201.32.200"
SERVER_PORT="5700"
BASE_URL="http://${SERVER_HOST}:${SERVER_PORT}"

echo "🔍 SPAM3UK Trim功能专项检查"
echo "=================================================="
echo "服务器: ${BASE_URL}"
echo ""

echo "1️⃣ 检查数据库中有哪些车型"
echo "----------------------------------------"
echo "获取所有车型列表..."
curl -s "$BASE_URL/api/vehicles" | jq -r '.[].model' | sort | uniq
echo ""

echo "2️⃣ 检查Focus车型的Trim数据"
echo "----------------------------------------"
echo "Focus车型Trim配置:"
curl -s "$BASE_URL/api/get_trims?model=Focus" | jq '.'
echo ""

echo "3️⃣ 检查数据库中实际的车型名称"
echo "----------------------------------------"
echo "从API获取的车型名称 (前10个):"
curl -s "$BASE_URL/api/vehicles?page=1&page_size=10" | jq -r '.[].model' | head -10
echo ""

echo "4️⃣ 检查具体车型详情"
echo "----------------------------------------"
echo "查看第一个车型的详细信息:"
curl -s "$BASE_URL/api/vehicles?page=1&page_size=1" | jq '.[0]' 
echo ""

echo "5️⃣ 检查Trim表中有哪些数据"
echo "----------------------------------------"
echo "尝试获取不同车型的Trim配置:"

# 尝试一些可能存在的车型名称
models=("Focus" "Fiesta" "Puma" "Kuga" "Mustang" "Transit" "Galaxy" "Explorer")

for model in "${models[@]}"; do
    echo -n "• $model: "
    response=$(curl -s -w "%{http_code}" -o /tmp/trim_check.json "$BASE_URL/api/get_trims?model=$model" 2>/dev/null)
    status_code="${response: -3}"
    
    if [ "$status_code" = "200" ]; then
        count=$(jq -r '.trims | length' /tmp/trim_check.json 2>/dev/null || echo "0")
        echo -e "${GREEN}✅ 找到 $count 个配置${NC}"
    else
        echo -e "${RED}❌ 未找到${NC}"
    fi
done

echo ""
echo "6️⃣ 验证服务器核心功能"
echo "----------------------------------------"
health_check=$(curl -s "$BASE_URL/health" | jq -r '.status' 2>/dev/null)
echo "健康状态: $health_check"

vehicles_count=$(curl -s "$BASE_URL/api/vehicles" | jq 'length' 2>/dev/null)
echo "车辆总数: $vehicles_count"

categories_count=$(curl -s "$BASE_URL/api/categories" | jq 'length' 2>/dev/null)
echo "分类总数: $categories_count"

echo ""
echo "📋 总结"
echo "----------------------------------------"
if [ "$vehicles_count" -gt 0 ]; then
    echo -e "${GREEN}✅ 主要API功能正常${NC}"
    echo -e "${GREEN}✅ 车辆数据完整 ($vehicles_count 个车辆)${NC}"
    echo -e "${GREEN}✅ 分类功能正常 ($categories_count 个分类)${NC}"
else
    echo -e "${RED}❌ 车辆数据可能有问题${NC}"
fi

echo ""
echo -e "${YELLOW}⚠️ Trim功能说明:${NC}"
echo "• Trim功能依赖于特定的车型数据"
echo "• 可能只有部分车型(如Focus)有Trim配置数据"
echo "• Puma、Mustang等车型可能需要先抓取数据"
echo ""

# 清理临时文件
rm -f /tmp/trim_check.json
